﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApAPPTaskManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public class CApAPPTaskManager
{
  private List<CApAPPTask> m_tasks;
  private bool m_isObjectFlowTask = true;
  private bool m_containsWeldOperation;

  public CApAPPTaskManager(TxObjectList i_operations)
  {
    this.m_tasks = new List<CApAPPTask>();
    KiAppCellPositioningDescriptorEx i_defDescriptor = (KiAppCellPositioningDescriptorEx) null;
    this.m_containsWeldOperation = false;
    foreach (ITxOperation iOperation in (Collection<ITxObject>) i_operations)
    {
      if (iOperation is ITxRoboticOrderedCompoundOperation)
      {
        if ((iOperation as ITxRoboticOrderedCompoundOperation).Robot != null)
        {
          this.m_tasks.Add((CApAPPTask) new CApAPPRoboticTask(iOperation as ITxRoboticOrderedCompoundOperation, ref i_defDescriptor));
          this.m_isObjectFlowTask = false;
          if (iOperation is ITxWeldOperation)
            this.m_containsWeldOperation = true;
        }
        else
        {
          if (iOperation is ITxWeldOperation i_gunOnlyWeldOperation)
            this.m_containsWeldOperation = true;
          this.m_tasks.Add((CApAPPTask) new CApAPPGunOnlyObjectFlowTask((ITxOperation) i_gunOnlyWeldOperation));
        }
      }
      else if (iOperation is TxObjectFlowOperation)
        this.m_tasks.Add((CApAPPTask) new CApAPPObjectFlowTask((ITxOperation) (iOperation as TxObjectFlowOperation)));
    }
    foreach (CApAPPTask task in this.m_tasks)
    {
      if (task is CApAPPRoboticTask)
      {
        CApAPPRoboticTask capAppRoboticTask = task as CApAPPRoboticTask;
        for (int i_index = 0; i_index < (task as CApAPPRoboticTask).CountWaypoints(); ++i_index)
        {
          KiAppCellPositioningDescriptorEx positioningDescriptor1 = capAppRoboticTask.Waypoint(i_index).KiWaypoint.BeforeWaypointCellPositioningDescriptor;
          capAppRoboticTask.UpdateDescriptorWithDefaultValues(positioningDescriptor1, i_defDescriptor);
          capAppRoboticTask.Waypoint(i_index).KiWaypoint.BeforeWaypointCellPositioningDescriptor = positioningDescriptor1;
          KiAppCellPositioningDescriptorEx positioningDescriptor2 = (task as CApAPPRoboticTask).Waypoint(i_index).KiWaypoint.AfterWaypointCellPositioningDescriptor;
          capAppRoboticTask.UpdateDescriptorWithDefaultValues(positioningDescriptor2, i_defDescriptor);
          capAppRoboticTask.Waypoint(i_index).KiWaypoint.AfterWaypointCellPositioningDescriptor = positioningDescriptor2;
        }
      }
    }
  }

  public List<CApAPPTask> Tasks
  {
    get => this.m_tasks;
    set => this.m_tasks = value;
  }

  public bool IsObjectFlowMode => this.m_isObjectFlowTask;

  public bool ContainsWeldOperation => this.m_containsWeldOperation;

  public int CountTasks() => this.Tasks.Count;

  public CApAPPTask RetrieveTaskFromOperation(ITxOperation i_op)
  {
    CApAPPTask capAppTask = (CApAPPTask) null;
    for (int index = 0; index < this.Tasks.Count; ++index)
    {
      capAppTask = this.Tasks[index];
      if (capAppTask.Operation == i_op)
      {
        capAppTask = this.Tasks[index];
        break;
      }
    }
    return capAppTask;
  }

  public bool retrieveWaypointIndexFromOperation(
    ITxOperation i_op,
    ref int o_indexTask,
    ref int o_indexWaypoint)
  {
    bool flag = false;
    for (int index1 = 0; index1 < this.Tasks.Count; ++index1)
    {
      TxObjectList allLocations = this.Tasks[index1].GetAllLocations();
      for (int index2 = 0; index2 < ((Collection<ITxObject>) allLocations).Count; ++index2)
      {
        if (((Collection<ITxObject>) allLocations)[index2] == i_op)
        {
          o_indexTask = index1;
          o_indexWaypoint = index2;
          flag = true;
          break;
        }
      }
    }
    return flag;
  }

  public void UpdateActiveSection(bool i_activeSection)
  {
    for (int index = 0; index < this.m_tasks.Count; ++index)
    {
      bool activeSectionVolumes = this.m_tasks[index].GetConsiderActiveSectionVolumes();
      this.m_tasks[index].SetConsiderActiveSectionVolumes(i_activeSection);
      if (activeSectionVolumes != i_activeSection)
        this.m_tasks[index].UpdateWaypointsStatus();
    }
  }

  public void UpdateCheckCableTwisting(bool i_activate, double i_maxAbsJ4J6)
  {
    for (int index = 0; index < this.m_tasks.Count; ++index)
    {
      if (this.m_tasks[index] is CApAPPRoboticTask)
      {
        if (i_activate)
          (this.m_tasks[index] as CApAPPRoboticTask).EnableCableTwistingCheck(i_maxAbsJ4J6);
        else
          (this.m_tasks[index] as CApAPPRoboticTask).DisableCableTwistingCheck();
      }
    }
  }

  public void UpdateActiveSectionAndIncludeDevice(bool i_activeSection)
  {
    for (int index = 0; index < this.m_tasks.Count; ++index)
    {
      bool activeSectionVolumes = this.m_tasks[index].GetConsiderActiveSectionVolumes();
      bool volumesForDevice = this.m_tasks[index].GetConsiderActiveSectionVolumesForDevice();
      this.m_tasks[index].SetConsiderActiveSectionVolumes(i_activeSection);
      this.m_tasks[index].SetConsiderActiveSectionVolumesForDevice(i_activeSection);
      if (activeSectionVolumes != i_activeSection || volumesForDevice != i_activeSection)
        this.m_tasks[index].UpdateWaypointsStatus();
    }
  }

  public void UpdateActiveSectionForDevice(bool i_activeSection)
  {
    for (int index = 0; index < this.m_tasks.Count; ++index)
    {
      bool volumesForDevice = this.m_tasks[index].GetConsiderActiveSectionVolumesForDevice();
      this.m_tasks[index].SetConsiderActiveSectionVolumesForDevice(i_activeSection);
      if (i_activeSection != volumesForDevice)
        this.m_tasks[index].UpdateWaypointsStatus();
    }
  }

  public void UpdateWaypointsStatus()
  {
    for (int index = 0; index < this.m_tasks.Count; ++index)
      this.m_tasks[index].UpdateWaypointsStatus();
  }

  public void Clear()
  {
    for (int index = 0; index < this.m_tasks.Count; ++index)
      this.m_tasks[index].ClearTask();
    this.m_tasks.Clear();
  }
}
